import { useEffect } from 'react';
import { useLocation } from 'umi';

const UseScroolIntoView = () => {
  const { hash } = useLocation();

  useEffect(() => {
    const id = hash.split('?')[0].slice(1);
    document.documentElement.scrollTop = 0;
    const element = document.getElementById(id || '');

    if (element) {
      element.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' });
    }
  }, [hash]);
};

export default UseScroolIntoView;
